home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / vector / ShortVec.h < prev    next >
C/C++ Source or Header  |  1990-05-16  |  10KB  |  282 lines

  1. #ifndef    SHORTVEC_H
  2. #define    SHORTVEC_H
  3.  
  4. /* ShortVec.h -- Short Integer Vectors
  5.  
  6.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  7.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  8.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  9.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  10.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  11.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  12.  
  13. Author:
  14.         Ted Persky
  15.     Bg. 12A, Rm. 2031
  16.     Computer Systems Laboratory
  17.     Division of Computer Research and Technology
  18.     National Institutes of Health
  19.     Bethesda, Maryland 20892
  20.     Phone: (301) 496-2963
  21.     uucp: uunet!nih-csl!tpersky
  22.     Internet:tpersky@alw.nih.gov
  23.  
  24. Modification History:
  25.  
  26. $Log:    ShortVec.h,v $
  27.  * Revision 3.0  90/05/16  23:00:39  kgorlen
  28.  * Release for 1st edition.
  29.  * 
  30. */
  31. #include "Vector.h"
  32. #include "BitVec.h"
  33. #include "IntVec.h"
  34.  
  35. class ShortSlice;
  36. class ShortPick;
  37. class ShortSlct;
  38.  
  39. class ShortVec : public Vector {
  40.     DECLARE_MEMBERS(ShortVec);
  41.     short* v;        // pointer to data, NULL if empty vector
  42.     void indexRangeErr() const;
  43. protected:
  44.     virtual void storer(OIOofd&) const;
  45.     virtual void storer(OIOout&) const;
  46. public:
  47.     ShortVec(unsigned len =0);
  48.     ShortVec(unsigned len, short from, short by =1);
  49.     ShortVec(const short*, unsigned len);
  50.     ShortVec(const ShortVec&);
  51.     ShortVec(const ShortSlice&);
  52.     ~ShortVec()            { delete v; }
  53.     ShortSlice operator()(int pos, unsigned lgt, int stride =1);
  54.     const ShortSlice operator()(int pos, unsigned lgt, int stride =1) const;
  55.     short* pt()        { return v; }
  56.     const short* pt() const    { return v; }
  57.     operator ShortSlice();
  58.     operator const ShortSlice() const;
  59.     operator DoubleVec();
  60. //    operator LongVec();
  61.     short& operator[](int i) {    // vector element
  62.         if ((unsigned)i >= n) indexRangeErr();
  63.         return v[i];
  64.     }
  65.     const short& operator[](int i) const {    // vector element
  66.         if ((unsigned)i >= n) indexRangeErr();
  67.         return v[i];
  68.     }
  69.     short& operator()(int i)            { return v[i]; }
  70.     const short& operator()(int i) const  { return v[i]; }
  71.     ShortPick operator[](const IntVec&);
  72.     const ShortPick operator[](const IntVec&) const;
  73.     ShortSlct operator[](const BitVec&);
  74.     const ShortSlct operator[](const BitVec&) const;
  75.     void /*ShortVec::*/operator=(const ShortVec&);
  76.     void /*ShortVec::*/operator=(const ShortSlice&);
  77.     void /*ShortVec::*/operator=(const ShortSlct&);
  78.     void /*ShortVec::*/operator=(const ShortPick&);
  79.     void /*ShortVec::*/operator=(short);
  80.     void /*ShortVec::*/lengthErr(const ShortSlice&) const;
  81.     void selectErr(const BitVec&) const;
  82.     virtual void deepenShallowCopy();
  83.     virtual unsigned hash() const;
  84.     virtual bool isEqual(const Object&) const;
  85.     virtual void printOn(ostream& strm =cout) const;
  86.     virtual void scanFrom(istream& strm);
  87.     virtual void sort();
  88.     virtual const Class* species() const;
  89. };
  90.  
  91. class TempShortVec : public ShortVec {
  92.     friend ShortSlice;
  93.     friend ShortPick;
  94.     friend ShortSlct;
  95.     TempShortVec(unsigned len =0) : ShortVec(len) {}
  96.     virtual void free();
  97. };
  98.  
  99. class ShortSlice : public NIHCL {
  100.     ShortVec* V;    // vector pointer
  101.     short* p;    // slice pointer
  102.     unsigned l;    // slice length
  103.     int k;    // slice stride
  104.     ShortSlice(const ShortVec& v, int pos, unsigned lgt, int stride =1);
  105.     ShortSlice(const ShortVec& v, unsigned lgt) {
  106.         V = &(ShortVec&)v;  p = ((ShortVec&)v).pt();  l = lgt;  k = 1;
  107.     }
  108.     ShortSlice(const ShortSlice&);
  109.     friend ShortVec;
  110. public:
  111.     ShortSlice(const ShortPick&);
  112.     ShortSlice(const ShortSlct&);
  113.     ~ShortSlice()        { V->free(); }
  114.     short* pt()        { return p; }
  115.     const short* pt() const    { return p; }
  116.     unsigned length() const    { return l; }
  117.     int stride() const    { return k; }
  118.     void /*ShortSlice::*/operator=(const ShortVec&);
  119.     void /*ShortSlice::*/operator=(const ShortPick&);
  120.     void /*ShortSlice::*/operator=(const ShortSlct&);
  121.     void /*ShortSlice::*/operator=(const ShortSlice&);
  122.     void /*ShortSlice::*/operator=(short);
  123.     void /*ShortSlice::*/lengthErr(const ShortVec&) const;
  124.     void /*ShortSlice::*/lengthErr(const ShortSlice&) const;
  125.     void /*ShortSlice::*/lengthErr(const IntVec&) const;
  126.     void selectErr(const BitVec&) const;
  127. friend    ShortVec    operator-(const ShortSlice&);
  128. friend    ShortVec    operator!(const ShortSlice&);
  129. friend    ShortVec    operator~(const ShortSlice&);
  130. friend    ShortVec    operator++(ShortSlice&);
  131. friend    ShortVec    operator--(ShortSlice&);
  132. friend    ShortVec    operator*(const ShortSlice&,const ShortSlice&);
  133. friend    ShortVec    operator/(const ShortSlice&,const ShortSlice&);
  134. friend    ShortVec    operator%(const ShortSlice&,const ShortSlice&);
  135. friend    ShortVec    operator+(const ShortSlice&,const ShortSlice&);
  136. friend    ShortVec    operator-(const ShortSlice&,const ShortSlice&);
  137. friend    ShortVec    operator&(const ShortSlice&,const ShortSlice&);
  138. friend    ShortVec    operator^(const ShortSlice&,const ShortSlice&);
  139. friend    ShortVec    operator|(const ShortSlice&,const ShortSlice&);
  140. friend    ShortVec    operator*(const ShortSlice&,short);
  141. friend    ShortVec    operator*(short s,const ShortSlice& V)  { return V*s; }
  142. friend    ShortVec    operator/(const ShortSlice&,short);
  143. friend    ShortVec    operator/(short,const ShortSlice&);
  144. friend    ShortVec    operator%(const ShortSlice&,short);
  145. friend    ShortVec    operator%(short,const ShortSlice&);
  146. friend    ShortVec    operator+(const ShortSlice&,short);
  147. friend    ShortVec    operator+(short s,const ShortSlice& V)  { return V+s; }
  148. friend    ShortVec    operator-(const ShortSlice&,short);
  149. friend    ShortVec    operator-(short,const ShortSlice&);
  150. friend    ShortVec    operator&(const ShortSlice&,short);
  151. friend    ShortVec    operator&(short s,const ShortSlice& V)  { return V&s; }
  152. friend    ShortVec    operator^(const ShortSlice&,short);
  153. friend    ShortVec    operator^(short s,const ShortSlice& V)  { return V^s; }
  154. friend    ShortVec    operator|(const ShortSlice&,short);
  155. friend    ShortVec    operator|(short s,const ShortSlice& V)  { return V|s; }
  156. friend    BitVec    operator<(const ShortSlice&,const ShortSlice&);
  157. friend    BitVec    operator>(const ShortSlice& U,const ShortSlice& V)    { return V < U; }
  158. friend    BitVec    operator<=(const ShortSlice&,const ShortSlice&);
  159. friend    BitVec    operator>=(const ShortSlice& U,const ShortSlice& V) { return V <= U; }
  160. friend    BitVec    operator==(const ShortSlice&,const ShortSlice&);
  161. friend    BitVec    operator!=(const ShortSlice&,const ShortSlice& V);
  162. friend    BitVec    operator<(const ShortSlice&,short);
  163. friend    BitVec    operator<(short s,const ShortSlice& V)  { return V > s; }
  164. friend    BitVec    operator>(const ShortSlice&,short);
  165. friend    BitVec    operator>(short s,const ShortSlice& V)  { return V < s; }
  166. friend    BitVec    operator<=(const ShortSlice&,short);
  167. friend    BitVec    operator<=(short s,const ShortSlice& V) { return V >= s; }
  168. friend    BitVec    operator>=(const ShortSlice&,short);
  169. friend    BitVec    operator>=(short s,const ShortSlice& V) { return V <= s; }
  170. friend    BitVec    operator==(const ShortSlice&,short);
  171. friend    BitVec    operator==(short s,const ShortSlice& V) { return V == s; }
  172. friend    BitVec    operator!=(const ShortSlice&,short);
  173. friend    BitVec    operator!=(short s,const ShortSlice& V) { return V != s; }
  174. friend    void    operator+=(ShortSlice&,const ShortSlice&);
  175. friend    void    operator+=(ShortSlice&,short);
  176. friend    void    operator-=(ShortSlice&,const ShortSlice&);
  177. friend    void    operator-=(ShortSlice&,short);
  178. friend    void    operator*=(ShortSlice&,const ShortSlice&);
  179. friend    void    operator*=(ShortSlice&,short);
  180. friend    void    operator/=(ShortSlice&,const ShortSlice&);
  181. friend    void    operator/=(ShortSlice&,short);
  182. friend    void    operator%=(ShortSlice&,const ShortSlice&);
  183. friend    void    operator%=(ShortSlice&,short);
  184. friend    void    operator&=(ShortSlice&,const ShortSlice&);
  185. friend    void    operator&=(ShortSlice&,short);
  186. friend    void    operator^=(ShortSlice&,const ShortSlice&);
  187. friend    void    operator^=(ShortSlice&,short);
  188. friend    void    operator|=(ShortSlice&,const ShortSlice&);
  189. friend    void    operator|=(ShortSlice&,short);
  190. friend    ShortVec    abs(const ShortSlice& V);
  191. friend    ShortVec    cumsum(const ShortSlice&);
  192. friend    ShortVec    delta(const ShortSlice&);
  193. friend    short    dot(const ShortSlice&,const ShortSlice&);
  194. friend    int    max(const ShortSlice&);
  195. friend    int    min(const ShortSlice&);
  196. friend    short    prod(const ShortSlice&);
  197. friend    ShortVec    reverse(const ShortSlice&);
  198. friend    short    sum(const ShortSlice&);
  199. };
  200.  
  201. class ShortPick : public NIHCL {
  202.     ShortVec* V;
  203.     const IntVec* X;
  204.     ShortPick(const ShortVec& v,const IntVec& x)    { V = &(ShortVec&)v;  X = &x; }
  205.     ShortPick(const ShortPick& s)            { V = s.V; X = s.X; }
  206.     friend ShortVec;
  207.     friend ShortSlice;
  208.     friend ShortSlct;
  209. public:
  210.     void /*ShortPick::*/operator=(const ShortVec&);
  211.     void /*ShortPick::*/operator=(const ShortPick&);
  212.     void /*ShortPick::*/operator=(const ShortSlct&);
  213.     void /*ShortPick::*/operator=(const ShortSlice&);
  214.     void /*ShortPick::*/operator=(short);
  215.     unsigned length() const    { return X->length(); }
  216. };
  217.  
  218. class ShortSlct : public NIHCL {
  219.     ShortVec* V;
  220.     const BitVec* B;
  221.     ShortSlct(const ShortVec& v, const BitVec& b)    { V = &(ShortVec&)v;  B = &b; }
  222.     ShortSlct(const ShortSlct& s)            { V = s.V; B = s.B; }
  223.     friend ShortVec;
  224.     friend ShortSlice;
  225.     friend ShortPick;
  226. public:
  227.     void /*ShortSlct::*/operator=(const ShortVec&);
  228.     void /*ShortSlct::*/operator=(const ShortPick&);
  229.     void /*ShortSlct::*/operator=(const ShortSlct&);
  230.     void /*ShortSlct::*/operator=(const ShortSlice&);
  231.     void /*ShortSlct::*/operator=(short);
  232.     unsigned length() const    { return B->length(); }
  233. };
  234.  
  235. inline ShortSlice ShortVec::operator()(int pos, unsigned lgt, int stride)
  236. {
  237.     ShortSlice s(*this,pos,lgt,stride);
  238.     return s;
  239. }
  240.  
  241. inline const ShortSlice ShortVec::operator()(int pos, unsigned lgt, int stride) const
  242. {
  243.     const ShortSlice s(*this,pos,lgt,stride);
  244.     return s;
  245. }
  246.  
  247. inline ShortVec::operator ShortSlice()
  248. {
  249.     ShortSlice s(*this,length());
  250.     return s;
  251. }
  252.  
  253. inline ShortVec::operator const ShortSlice() const
  254. {
  255.     const ShortSlice s(*this,length());
  256.     return s;
  257. }
  258.  
  259. inline ShortPick ShortVec::operator[](const IntVec& I)
  260. {
  261.     return ShortPick(*this,I);
  262. }
  263.  
  264. inline const ShortPick ShortVec::operator[](const IntVec& I) const
  265. {
  266.     const ShortPick t(*this,I);
  267.     return t;
  268. }
  269.  
  270. inline ShortSlct ShortVec::operator[](const BitVec& B)
  271. {
  272.     return ShortSlct(*this,B);
  273. }
  274.  
  275. inline const ShortSlct ShortVec::operator[](const BitVec& B) const
  276. {
  277.     const ShortSlct t(*this,B);
  278.     return t;
  279. }
  280.  
  281. #endif
  282.